<?php
/**
 * 供应商银行帐号信息审核管理
 * Created by PhpStorm.
 * User: z
 * Date: 14-9-1
 * Time: 下午8:52
 */
class Controller_Suppliercheck extends Controller_Admin
{
    /**
     * 权限检查
     */
    public function before()
    {
        try{
            ACL::Required('supplier bankinfo check view');
        }catch(HTTP_Exception_403 $e){
            Message::error('sorry，你没有权限进这个目录');
            $this->request->redirect(URL::base(TRUE));
        }
        parent::before();
    }

    /**
     * 显示需要审核供应商信息
     */
    public function action_index()
    {
        $this->title = __('审核供应商信息');

        try{

            $payway_all = Payway::get_payway_lists_active();
            $coins_arr = supplierbank::getCoins_arr();//币种信息
            $condtion = array();

            if(isset($_GET['supplier_id'])
                and intval($_GET['supplier_id']) > 0)
            {
                $condtion['supplier_id'] = intval($_GET['supplier_id']);
            }
            if(isset($_GET['supplier_name'])
                and trim($_GET['supplier_name']))
            {
                $condtion['supplier_name'] = trim($_GET['supplier_name']);
            }
            if(isset($_GET['payways_id'])
                and intval($_GET['payways_id']) > 0)
            {
                $condtion['payways_id'] = intval($_GET['payways_id']);
            }
            if(isset($_GET['active_status'])
                and intval($_GET['active_status']) > 0)
            {
                $condtion['active_status'] = intval($_GET['active_status']);
            }
            if(isset($_GET['check_status'])
                and intval($_GET['check_status']) > 0)
            {
                $condtion['check_status'] = intval($_GET['check_status']);
            }

            if(isset($_GET['coinid'])
                and intval($_GET['coinid']) > 0)
            {
                $condtion['coinid'] = intval($_GET['coinid']);
            }

            if(isset($_GET['bankname'])
                and trim($_GET['bankname']))
            {
                $condtion['bankname'] = trim($_GET['bankname']);
            }
            if(isset($_GET['bankaccount'])
                and trim($_GET['bankaccount']))
            {
                $condtion['bankaccount'] = trim($_GET['bankaccount']);
            }
            if(isset($_GET['bankaccountname'])
                and trim($_GET['bankaccountname']))
            {
                $condtion['bankaccountname'] = trim($_GET['bankaccountname']);
            }
            $count = count(Supplierbank::do_select($condtion));
            $pagination         = Pagination::factory(array(
                'items_per_page'  => 20,
                'total_items'     => $count,
                'uri'	            => $this->request->uri(),
            ));

            $condtion['limit'] = $pagination->items_per_page;
            $condtion['offset'] = $pagination->_offset;

            $re_lists = Supplierbank::do_select($condtion);//var_dump($re_lists);
            $re_lists = Supplierbank::datas_to_views($re_lists);
        }
        catch(Database_Exception $e)
        {
            $re_lists = NULL;
            Message::error('系统出错,请联系IT！');
        }

        $view = View::factory('suppliercheck/index');
        $view->set('payway_all',$payway_all);
        $view->set('re_lists',$re_lists);
        $view->set('condtion',$condtion);
        $view->set('pagination',$pagination);
        $view->set('coins_arr',$coins_arr);
        $this->response->body($view);
    }

    /**
     * 改变供应商审核状态
     */
    public function action_checkstatus()
    {
        $this->title = __('修改供应商信息');


        if($this->request->is_get()
            and isset($_GET['id'])
            and intval($_GET['id']) > 0)
        {

            try{
                $datail_data = Supplierbank::do_select(array('id'=>trim($_GET['id'])));
                $datail_data = Supplierbank::datas_to_views($datail_data);

                if(empty($datail_data))
                {
                    Message::error('查询不到数据');
                    $this->request->redirect('supplier/suppliercheck/index');
                }

            }
            catch(Database_Exception $e)
            {
                Message::error('系统出错,请联系IT！');
                $this->request->redirect('supplier/suppliercheck/index');
            }
        }
        else
        {
            Message::error('查询不到数据');
            $this->request->redirect('supplier/suppliercheck/index');
        }

        $view = View::factory('suppliercheck/checkstatus');
        $view->set('detail_data',isset($datail_data[0])?$datail_data[0]:NULL);
        $this->response->body($view);
    }

    //删除供应商账号
    public function action_delete_bank()
    {
        if($this->request->is_get()
            and isset($_GET['id'])
            and intval($_GET['id']) > 0)
        {

            try{
                $payaccountinfo_id=DB::select('payaccountinfo_id')->from('supplierpayaccount')
                    ->where('id','=',$_GET['id'])
                    ->execute('newerp')
                    ->get('payaccountinfo_id',0);

                DB::delete('payaccountinfo')->where('id','=',$payaccountinfo_id)->execute('newerp');
                DB::delete('supplierpayaccount')->where('id','=',$_GET['id'])->execute('newerp');


                if(empty($datail_data))
                {
                    Message::error('查询不到数据');
                    $this->request->redirect('supplier/suppliercheck/index');
                }

            }
            catch(Database_Exception $e)
            {
                Message::error('系统出错,请联系IT！');
                $this->request->redirect('supplier/suppliercheck/index');
            }
        }
        else
        {
            Message::error('查询不到数据');
            $this->request->redirect('supplier/suppliercheck/index');
        }

        $view = View::factory('suppliercheck/checkstatus');
        $this->response->body($view);
    }

    /**
     * 显示审核信息全貌
     */
    public function action_docheck()
    {
        if($this->request->is_post()
            and isset($_POST['id'])
            and isset($_POST['check_status']))
        {

            try{

                $supplierpayaccount = array();
                $supplierpayaccount_id = intval($_POST['id']);
                $supplierpayaccount['check_status'] = intval($_POST['check_status']);
                $supplierpayaccount['check_id'] = User::active_user()->id;
                $supplierpayaccount['check_remark'] = (isset($_POST['check_remark']) and trim($_POST['check_remark']))?trim($_POST['check_remark']):NULL;
                $supplierpayaccount['updatetime'] = date("Y-m-d H:i:s");
                $re_ok = DB::update('supplierpayaccount')
                    ->set($supplierpayaccount)
                    ->where('id','=',$supplierpayaccount_id)
                    ->execute('newerp');

                if($re_ok)
                {
                      Message::success('操作成功');
                    $this->request->redirect('supplier/suppliercheck/checkstatus?id='.$supplierpayaccount_id);
                }

            }catch (Database_Exception $e)
            {
                Message::error('系统出错,请联系IT！');
            }
            $this->request->redirect('supplier/suppliercheck/index');
        }

    }
}